package com.focus.xyz.other.pages.tpytb.baby;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BasePage;
import com.focus.xyz.core.utils.CookieUtils;
import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

/**
 * Created by chenmeng on 2016/5/16.
 */
public class MainPage extends BasePage {
    private String url = "http://ebiz.cpic.com.cn/ywx/sysLogin.do?method=isLogin";
    private Action action;
    private WebDriver driver;

    @FindBy(name = "passPort")
    private WebElement username;
    @FindBy(name = "passWord")
    private WebElement password;
    @FindBy(name = "validate")
    private WebElement code;

    @FindBy(id = "Image1")
    private WebElement loginBtn;


    public MainPage(WebDriver driver, Action action) {
        super(driver, action);
        this.action = action;
        this.driver = driver;
        super.url = this.url;
        PageFactory.initElements(this.driver,this);
    }



    public boolean isLogined() throws Exception{
        boolean flag = false;
        if(action.isElementExsit(By.xpath("//input[@id=\"Image1\"]"))){
            flag = false;
        }else{
            flag = true;
        }
        return flag;
    }



    public void login(String userName,String pass) throws Exception{
        action.inputText(username,userName);
        action.inputText(password,pass);
        action.pause(5000l);
        action.click(loginBtn);

    }
    public void loginSystem(String userName,String pass) throws Exception{
        openPage();
        CookieUtils.addCookie(driver, ConfigPropInfo.TEST_DATA_DIR + "cookies/" + ConfigPropInfo.BABY_CARD_COOKIE_FILE);
        openPage();
        if (!isLogined()) {
            login(userName, pass);
            CookieUtils.saveCookie(driver, ConfigPropInfo.TEST_DATA_DIR + "cookies/" + ConfigPropInfo.BABY_CARD_COOKIE_FILE,".ebiz.cpic.com.cn");
            action.waitForPageLoad();
        }
    }




}
